package com.kwin.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

public class SessionApp22 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Book book = DB.getAll().get(id);
        HttpSession httpSession = req.getSession();
        List list = (List) httpSession.getAttribute("list");

        if (list == null) {
            list = new ArrayList<>();
            httpSession.setAttribute("list", list);
        }

        list.add(book);

        String url = "/SessionApp23";
        resp.sendRedirect(url);
//        resp.sendRedirect(resp.encodeURL(url));//禁用cookie后重写url
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
